/* Server Main
 * Main point of entry to the server.
 * waits for clients to connect
 * creates a client structure and adds them to the server manager
 */

package sbcc.edu.blackjack.server;

import java.io.IOException;
import java.net.ServerSocket;

import sbcc.edu.blackjack.Player;
import sbcc.edu.blackjack.ServerIOThread;
import sbcc.edu.blackjack.command.CommandQueue;

public class ServerMain {
	
	
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        ServerOperator boss = new ServerOperator();
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
        System.out.println("Server Running...");
        while (listening){
	    //create client structure and start its thread
        	CommandQueue cq = new CommandQueue();
        	new ServerIOThread(serverSocket.accept(), cq).start();
        	System.out.println("New Client");
        	Player player = new Player(cq);
        	boss.addPlayerToTable(player);
        }

        serverSocket.close();
    }
}
